#pragma once
#include "ProbabilityModel.h"
#include "ContextTree.h"
#include "BitBuffer.h"

class ArithmeticDecoder
{
	public:
		ArithmeticDecoder(ProbabilityModel* model, unsigned int outputSize = 1024);
		ArithmeticDecoder(ContextTree* tree, int modelSize, int blockSize, unsigned int outputSize = 1024);
		virtual ~ArithmeticDecoder(void);
		char* decodeBitStream(BitBuffer& buffer, unsigned long long symbolsToDecode);

	private:
		void initializeDecoder(BitBuffer& buffer);
		unsigned int decodeSymbol(BitBuffer& buffer);
		void normalizeRegisters(unsigned int symbol, BitBuffer& buffer);

		ProbabilityModel* model;
		ContextTree* ctwTree;
		
		char* result;
		unsigned int code;
		unsigned int lowerBound;
		unsigned int upperBound;
		unsigned short underflow;
		unsigned short bitsInRegister;
		unsigned short symbolSize;
		int modelSize;
		int blockSize;
};

